﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using ChiDao.Component.Utils;
using ChiDao.Business.Models;
using ChiDao.Business.Models.FlowProcess;
using ChiDao.Site.Models;
using ChiDao.Business.Data.Repositories.FlowProcess;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IBaklogTaskService))]
    internal class BaklogTaskService : IBaklogTaskService
    {
        [Import]
        protected IBaklogTaskRepository BaklogTaskRepository { get; set; }

        public List<BaklogTaskView> QueryBaklogTask(string processPerson, string processID = "", string HandleState="0")
        {
            List<BaklogTaskView> baklogTaskViewList = new List<BaklogTaskView>();
            IQueryable<BaklogTask> baklogTasks = BaklogTaskRepository.Entities.Where(c => c.CurrentProcessPerson == processPerson && c.HandleState == "0");
            if (!string.IsNullOrEmpty(processID))
            {
                baklogTasks = BaklogTaskRepository.Entities.Where(c => c.ProcessID == processID && c.HandleState == HandleState);
            }
            foreach (BaklogTask baklogTask in baklogTasks)
            {
                baklogTaskViewList.Add(new BaklogTaskView()
                {
                    ID = baklogTask.ID,
                    ProcessID = baklogTask.ProcessID,
                    TaskName = baklogTask.TaskName,
                    RequestPerson = baklogTask.RequestPerson,
                    CurrentProcessNodeCode =baklogTask.CurrentProcessNodeCode,
                    CurrentProcessNodeName = baklogTask.CurrentProcessNodeName,
                    NextProcessNodeCode = baklogTask.NextProcessNodeCode,
                    ApprovalOpinion = baklogTask.ApprovalOpinion,
                    CurrentProcessPerson = baklogTask.CurrentProcessPerson,
                    ProcessTime = baklogTask.ProcessTime
                });
            }
            return baklogTaskViewList;
        }

        public string GetNextProcessNodeCode(string processPerson, string processID, ref string currentProcessNodeCode)
        {
            string nextProcessNodeCode = string.Empty;
            IQueryable<BaklogTask> baklogTasks = BaklogTaskRepository.Entities.Where(b => b.CurrentProcessPerson == processPerson && b.HandleState == "0" && b.ProcessID == processID);
            if (baklogTasks.Count() == 1)
            {
                currentProcessNodeCode = baklogTasks.First().CurrentProcessNodeCode;
                nextProcessNodeCode = baklogTasks.First().NextProcessNodeCode;
            }
            return nextProcessNodeCode;
        }

        public void AddBaklogTask(List<BaklogTaskView> addList)
        {
            foreach (BaklogTaskView baklogTaskView in addList)
            {
                BaklogTask baklogTask = new BaklogTask()
                {
                    ID = baklogTaskView.ID,
                    ProcessID = baklogTaskView.ProcessID,
                    TaskName = baklogTaskView.TaskName,
                    RequestPerson = baklogTaskView.RequestPerson,
                    CurrentProcessNodeCode = baklogTaskView.CurrentProcessNodeCode,
                    CurrentProcessNodeName = baklogTaskView.CurrentProcessNodeName,
                    NextProcessNodeCode = baklogTaskView.NextProcessNodeCode,
                    ApprovalOpinion = baklogTaskView.ApprovalOpinion,
                    HandleState = baklogTaskView.HandleState,
                    CurrentProcessPerson = baklogTaskView.CurrentProcessPerson,
                    ProcessTime = baklogTaskView.ProcessTime
                };
                BaklogTaskRepository.Insert(baklogTask);
            }
        }

        public void UpdateBaklogTask(List<BaklogTaskView> updateList)
        {
            foreach (BaklogTaskView baklogTaskView in updateList)
            {
                BaklogTask record = BaklogTaskRepository.Entities.FirstOrDefault(m => m.ID == baklogTaskView.ID);
                if (record != null)
                {
                    record.ApprovalOpinion = baklogTaskView.ApprovalOpinion;
                    record.HandleState = baklogTaskView.HandleState;
                    record.ProcessTime = baklogTaskView.ProcessTime;
                    BaklogTaskRepository.Update(record);
                }
            }
        }
    }
}